package com.ews.onlineexchange.dao;

import java.io.Serializable;

import javax.annotation.PreDestroy;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import com.ews.onlineexchange.model.DataEntity;

/**
 * Session Bean implementation class EntityManagerDao
 */
@Stateful
public class EntityManagerDao<T extends DataEntity> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@PersistenceContext(type = PersistenceContextType.EXTENDED)
    protected EntityManager entityManager;
 
    public Object updateObject(Object object) {
        Object instance = entityManager.merge(object);
        entityManager.flush();
		return instance;
    }
 
    public Object createObject(Object object) {
        entityManager.persist(object);
        entityManager.flush();
        return object;
    }
 
    public void refresh(Object object) {
        entityManager.refresh(object);
    }
 
    public <T> T find(Class<T> clazz, Object id) {
        return entityManager.find(clazz, id);
    }
    
    public void deleteObject(Object object) {
    	object = entityManager.merge(object);
        entityManager.remove(object);
    }
    
    @PreDestroy
    private void PreRemove() {}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

}
